/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package parsertojaklin;

import com.mysql.jdbc.Statement;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Hashtable;
import java.util.regex.Matcher;

/**
 *
 * @author Юра
 */
public class Wares  {
    static String table_name = "wares";
    static File dir = new File("D:\\projects\\optvideo\\optvideo\\optvideo.com\\new_design\\new_kat\\view\\");
    static String file_name = "view.php-kod=";   // wares

    public String id_wares="";
    public String partnumber="";
    public String id_vendor="";
    public String belong_groups="0";
    public String visibility="";
    public String description="";
    public String price="";
    public String price_old="";
    public String price_opt="";
    public String name_wares="";
    public String descript_wares="";
    
    Wares (){
    }

    Wares(String id_wares,  String name_wares, String price_opt, String visibility, String description, String descript_wares){
    this.id_wares = id_wares; 
    this.visibility = visibility; 
    this.description = description;
    this.price_opt = price_opt; 
    this.name_wares = name_wares; 
    this.descript_wares = descript_wares; 
    }

    public String insert_string() {
    String query =   "INSERT INTO `wares` (`id_wares`,`name_wares`,`belong_groups`,`visibility`,`description`,`price_opt`,`descript_wares`, `vendors`) VALUES";
    query += "('"+id_wares+"', '"+name_wares+"', '"+belong_groups+"', '"+visibility+"', '"+description+"', '"+price_opt+"', '"+descript_wares+"', (select id from vendors where `name_vendor`='"+id_vendor+"') )";
    System.out.print(query);
    return query;
    }

    public static  Hashtable<String, Wares> parse_wares(Statement s) throws SQLException, FileNotFoundException, IOException{
        Hashtable<String, Wares> wareses = new Hashtable<String, Wares>();
        s.executeUpdate ("TRUNCATE TABLE `"+ table_name+"`");
        s.executeUpdate ("TRUNCATE TABLE `descriptions`");

        String[] dir_list = dir.list();
        int c=1;

        for (String filename :dir_list)
            //if (filename.length()>=file_name.length() && filename.substring(0,file_name.length()).equals(file_name) )
            {
            Matcher matc = null;
            Wares war =  null;
                String id = filename.substring(file_name.length(),filename.length()-4);
                //parser.find_one(".{0,200}<title>(.{0,200})</title>.*<p style=\"font-size:14px;color:.{9}(.{0,20})</p>.*", dir+"\\"+filename, wareses, id);
                //parser.find_one(".{0,200}<title>(.{0,200})</title>.*<p style=\"font-size:14px;color:.{9}(.{0,20})</p>.*.{1000,1500}</head><body><table id=\"har\" style=\"border:none\"><tr><td valign=\"top\" style=\"border:none\">(.+)</body>.{60,80}", dir+"\\"+filename, wareses, id);
                matc = parser.find_in_file(".{0,200}<title>(.{0,200})</title>.*<p style=\"font-size:14px;color:.{9}(.{0,20}) .{4}</p>.{1000,1500}</head><body><table id=\"har\" style=\"border:none\"><tr><td valign=\"top\" style=\"border:none\">(.+)</body>.{60,80}", dir+"\\"+filename);
                if (matc.find())
                {
                    s.executeUpdate(Description.insert_string(id, matc.group(3)));
                    war =  new Wares(id, matc.group(1), matc. group(2), "1", "1", "");
                    System.out.println(Description.insert_string(id, matc.group(3)));
                }
                else
                {
                    matc = parser.find_in_file(".{0,200}<title>(.{0,200})</title>.*<p style=\"font-size:14px;color:.{9}(.{0,20}) .{4}</p>.*", dir+"\\"+filename);
                    if (matc.find()) war = new Wares(id, matc.group(1), matc. group(2), "1",  "0", "");
                }
                wareses.put(id, war);
                //war.belong_groups = ven.id_vendor;
                //war.id_vendor  = ven.name_vendor;

                //System.out.print(ven.id_vendor+"_"+ven.name_vendor);
                s.executeUpdate(war.insert_string());

                System.out.println(c++ +"-"+id);
                if (c>2500)
                    break;
            }
                


        return wareses;
    }
}
